home *** CD-ROM | disk | FTP | other *** search
- unit TestForm;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, PropertyIO;
-
- type
- TSex = (sxUnknown, sxMale, sxFemale);
- TPet = (Dog, Cat, Rabbit, Hamster);
- TPets = set of TPet;
-
- TPerson = class (TSQLObject)
- private
- FName: String;
- FAge: Integer;
- FSex: TSex;
- FPets: TPets;
- function GetPets: String;
- procedure SetPets (Value: String);
- published
- // properties with RTTI follow
- property Name: String read FName write FName;
- property Age: Integer read FAge write FAge;
- property Sex: TSex read FSex write FSex;
- property Pets: TPets read FPets write FPets stored False;
- property PetStr: String read GetPets write SetPets;
- end;
-
- TForm1 = class(TForm)
- btnExport: TButton;
- btnImport: TButton;
- pioImporter: TPropertyImporter;
- pioExporter: TPropertyExporter;
- btnExit: TButton;
- edtName: TEdit;
- edtAge: TEdit;
- cboSex: TComboBox;
- lblName: TLabel;
- lblAge: TLabel;
- lblSex: TLabel;
- btnSQLInsert: TButton;
- btnSQLUpdate: TButton;
- btnSQLSelect: TButton;
- grpPets: TGroupBox;
- chkDog: TCheckBox;
- chkCat: TCheckBox;
- chkRabbit: TCheckBox;
- chkHamster: TCheckBox;
- procedure btnExportClick(Sender: TObject);
- procedure btnImportClick(Sender: TObject);
- procedure pioImporterObjectImported(Sender: TObject);
- procedure btnExitClick(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure btnSQLInsertClick(Sender: TObject);
- procedure btnSQLUpdateClick(Sender: TObject);
- procedure btnSQLSelectClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDeactivate(Sender: TObject);
- private
- Person: TPerson;
- procedure PopulatePerson;
- public
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- TypInfo;
-
- {$R *.DFM}
-
- // TPerson
-
- function TPerson.GetPets: String;
- begin
- Result := '';
- if Dog in Pets then Result := Result + 'D';
- if Cat in Pets then Result := Result + 'C';
- if Rabbit in Pets then Result := Result + 'R';
- if Hamster in Pets then Result := Result + 'H';
- end;
-
- procedure TPerson.SetPets (Value: String);
- begin
- FPets := [];
- if Pos ('D', Value) <> 0 then FPets := FPets + [Dog];
- if Pos ('C', Value) <> 0 then FPets := FPets + [Cat];
- if Pos ('R', Value) <> 0 then FPets := FPets + [Rabbit];
- if Pos ('H', Value) <> 0 then FPets := FPets + [Hamster];
- end;
-
- // TForm1
-
- procedure TForm1.PopulatePerson;
- begin
- // name
- Person.Name := edtName.Text;
- // age
- try
- Person.Age := StrToInt (edtAge.Text);
- except
- on EConvertError do begin
- Person.Age := 0;
- end;
- end;
- // sex
- if cboSex.ItemIndex <> -1 then begin
- Person.Sex := TSex (cboSex.ItemIndex);
- end;
- // pets
- Person.Pets := [];
- if chkDog.Checked then Person.Pets := Person.Pets + [Dog];
- if chkCat.Checked then Person.Pets := Person.Pets + [Cat];
- if chkRabbit.Checked then Person.Pets := Person.Pets + [Rabbit];
- if chkHamster.Checked then Person.Pets := Person.Pets + [Hamster];
- end;
-
- procedure TForm1.btnExportClick(Sender: TObject);
- begin
- PopulatePerson;
- // write out the object
- pioExporter.ExportObject (Person);
- end;
-
- procedure TForm1.btnImportClick(Sender: TObject);
- begin
- RegisterClass (TPerson);
-
- pioImporter.ImportFile (pioExporter.FileName);
- end;
-
- procedure TForm1.pioImporterObjectImported(Sender: TObject);
- begin
- if Sender is TPerson then begin
- with Sender as TPerson do begin
- edtName.Text := Name;
- edtAge.Text := IntToStr (Age);
- cboSex.ItemIndex := Ord (Sex);
- chkDog.Checked := (Dog in Pets);
- chkCat.Checked := (Cat in Pets);
- chkRabbit.Checked := (Rabbit in Pets);
- chkHamster.Checked := (Hamster in Pets);
- end;
- end;
- end;
-
- procedure TForm1.btnExitClick(Sender: TObject);
- begin
- Application.Terminate;
- end;
-
- procedure TForm1.FormShow(Sender: TObject);
- begin
- cboSex.ItemIndex := 0;
- end;
-
- procedure TForm1.btnSQLInsertClick(Sender: TObject);
- begin
- PopulatePerson;
- ShowMessage (Person.SQLInsert)
- end;
-
- procedure TForm1.btnSQLUpdateClick(Sender: TObject);
- begin
- PopulatePerson;
- ShowMessage (Person.SQLUpdate)
- end;
-
- procedure TForm1.btnSQLSelectClick(Sender: TObject);
- begin
- PopulatePerson;
- ShowMessage (Person.SQLSelect)
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Person := TPerson.Create;
- end;
-
- procedure TForm1.FormDeactivate(Sender: TObject);
- begin
- Person.Free;
- end;
-
- end.
-